package com.linx.enterprise.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.linx.common.core.domain.YsyjRetrunVo;
import com.linx.common.core.domain.dto.OssDTO;
import com.linx.common.core.service.EnterpriseService;
import com.linx.common.core.service.OssService;
import com.linx.common.core.utils.MapstructUtils;
import com.linx.common.core.utils.StringUtils;
import com.linx.common.mybatis.core.page.PageQuery;
import com.linx.common.mybatis.core.page.TableDataInfo;
import com.linx.enterprise.domain.EnterpriseInfo;
import com.linx.enterprise.domain.bo.EnterpriseInfoBo;
import com.linx.enterprise.domain.vo.EnterpriseInfoVo;
import com.linx.enterprise.mapper.EnterpriseInfoMapper;
import com.linx.enterprise.service.IEnterpriseInfoService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 企业信息Service业务层处理
 *
 * @author linx
 * @date 2024-10-10
 */
@RequiredArgsConstructor
@Service
public class EnterpriseInfoServiceImpl implements IEnterpriseInfoService, EnterpriseService {

    private final EnterpriseInfoMapper baseMapper;
    private final OssService ossService;

    /**
     * 查询企业信息
     *
     * @param enterpriseInfoId 主键
     * @return 企业信息
     */
    @Override
    public EnterpriseInfoVo queryById(Long enterpriseInfoId){
        EnterpriseInfoVo enterpriseInfoVo = baseMapper.selectVoById(enterpriseInfoId);
        if (enterpriseInfoVo!= null) {
            List<OssDTO> ossDTOList = ossService.selectByBiz(enterpriseInfoId,null, EnterpriseInfo.class);

            // 补充图片信息
            enterpriseInfoVo.setBusinessLicenseImageId(ossDTOList.stream().filter(ossDTO -> Objects.equals(ossDTO.getBizCode(), ("business-license"))).findFirst().orElse(new OssDTO()).getOssId());
            enterpriseInfoVo.setLogoImageId(ossDTOList.stream().filter(ossDTO -> Objects.equals(ossDTO.getBizCode(), ("logo"))).findFirst().orElse(new OssDTO()).getOssId());
        }

        return enterpriseInfoVo;
    }

    /**
     * 分页查询企业信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 企业信息分页列表
     */
    @Override
    public TableDataInfo<EnterpriseInfoVo> queryPageList(EnterpriseInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EnterpriseInfo> lqw = buildQueryWrapper(bo);
        Page<EnterpriseInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的企业信息列表
     *
     * @param bo 查询条件
     * @return 企业信息列表
     */
    @Override
    public List<EnterpriseInfoVo> queryList(EnterpriseInfoBo bo) {
        LambdaQueryWrapper<EnterpriseInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EnterpriseInfo> buildQueryWrapper(EnterpriseInfoBo bo) {
        LambdaQueryWrapper<EnterpriseInfo> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getEnterpriseName()), EnterpriseInfo::getEnterpriseName, bo.getEnterpriseName());
        lqw.eq(StringUtils.isNotBlank(bo.getUscc()), EnterpriseInfo::getUscc, bo.getUscc());
        lqw.eq(StringUtils.isNotBlank(bo.getResponsiblePeople()), EnterpriseInfo::getResponsiblePeople, bo.getResponsiblePeople());
        lqw.eq(StringUtils.isNotBlank(bo.getTelephone()), EnterpriseInfo::getTelephone, bo.getTelephone());
        return lqw;
    }

    /**
     * 新增企业信息
     *
     * @param bo 企业信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(EnterpriseInfoBo bo) {
        EnterpriseInfo add = MapstructUtils.convert(bo, EnterpriseInfo.class);
        validEntityBeforeSave(add);

        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setEnterpriseInfoId(add.getEnterpriseInfoId());

            // 更新营业执照图片
            ossService.updateBiz(add.getEnterpriseInfoId(), EnterpriseInfo.class, "business-license", Collections.singletonList(bo.getBusinessLicenseImageId()));
            // 更新企业logo图片
            ossService.updateBiz(add.getEnterpriseInfoId(), EnterpriseInfo.class, "logo", Collections.singletonList(bo.getLogoImageId()));
        }
        return flag;
    }

    /**
     * 修改企业信息
     *
     * @param bo 企业信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(EnterpriseInfoBo bo) {
        EnterpriseInfo update = MapstructUtils.convert(bo, EnterpriseInfo.class);
        validEntityBeforeSave(update);

        // 更新营业执照图片
        ossService.updateBiz(bo.getEnterpriseInfoId(), EnterpriseInfo.class, "business-license", Collections.singletonList(bo.getBusinessLicenseImageId()));
        // 更新企业logo图片
        ossService.updateBiz(bo.getEnterpriseInfoId(), EnterpriseInfo.class, "logo", Collections.singletonList(bo.getLogoImageId()));

        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(EnterpriseInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除企业信息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    @Transactional
    public YsyjRetrunVo getYsyjEnterpriseInfo(List<EnterpriseInfo> enterpriseInfos) {
        boolean flag = baseMapper.insertOrUpdateBatch(enterpriseInfos);
        return flag ? YsyjRetrunVo.ok() : YsyjRetrunVo.fail("操作失败");
    }

    @Override
    public String getEnterpriseNameById(String enterpriseId) {
        EnterpriseInfo enterpriseInfo = baseMapper.selectById(enterpriseId);
        if (enterpriseInfo != null) {
            return enterpriseInfo.getEnterpriseName();
        }

        return "";
    }

}
